package yang.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yang.dao.BaseDaoI;
import yang.model.ChufangInfo;
import yang.model.FinanceInfo;
import yang.pageModel.DataGrid;
import yang.pageModel.Finance;
import yang.pageModel.Json;
import yang.service.FinanceServiceI;
/**
 * 
 * @ClassName: FinanceServiceImpl
 * @Description: 财务服务的实现
 * @author: Administrator
 * @date: 2014年7月12日 上午9:34:25
 */
@Service("financeService")
public class FinanceServiceImpl implements FinanceServiceI {
	private static final Logger logger = Logger
			.getLogger(FinanceServiceImpl.class);
	private BaseDaoI<FinanceInfo> financeDao;//调用财务的持久化类
	private BaseDaoI<ChufangInfo> chufangDao;//处方
	/**
	 * 
	 * @Title: saveFinance
	 * @Description: 保存财务信息
	 * @param finance
	 * @return 返回一个JSON格式
	 * @see yang.service.FinanceServiceI#saveFinance(yang.model.FinanceInfo)
	 */
	@Override
	public Json saveFinance(FinanceInfo finance) {
		Json json=new Json();
		try {
			financeDao.save(finance);
			json.setSuccess(true);
			json.setMsg("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.setMsg("服务器错误，请停止缴费");
		}
		return json;
	}
	/**
	 * 
	 * @Title: findAllFinance
	 * @Description: 查询所有财务信息
	 * @param finance
	 * @return
	 * @see yang.service.FinanceServiceI#findAllFinance(yang.pageModel.Finance)
	 */
	@Override
	public DataGrid findAllFinance(Finance finance) {
		String hql = "from FinanceInfo f where 1=1";// 查询语句
		logger.info("建立了查询语句");
		DataGrid dataGrid = new DataGrid();// 创建DATAGRID
		Map<String, Object> params = new HashMap<String, Object>();// 查询条件
		hql = addWhere(finance, hql, params);// 为语句增加WHERE条件
		String totalHql = "select count(*) " + hql;// 查询总数的语句
		hql = addOrder(finance, hql);// 为查询语句增加ORDER条件
		List<FinanceInfo> list = financeDao.find(hql, params,
				finance.getPage(), finance.getRows());// 进行查询
		List<Finance> list2 = new ArrayList<Finance>();// 建立模型
		changeModel(list, list2);// 为模型赋值
		dataGrid.setRows(list2);// 向前台输出了
		dataGrid.setTotal(financeDao.count(totalHql, params));
		logger.info("返回ACTION");
		return dataGrid;
	}

	/**
	 * @Title: changeModel
	 * @Description: 为模型赋值
	 * @param list
	 * @param list2
	 * @return: void
	 */
	private void changeModel(List<FinanceInfo> list, List<Finance> list2) {
		if (list != null && list.size() > 0) {
			for (FinanceInfo info : list) {
				Finance finance = new Finance();
				BeanUtils.copyProperties(info, finance);
				list2.add(finance);
				logger.info("完成赋值");
			}
		}
	}

	/**
	 * 
	 * @方法:增加ORDER条件
	 * @author:阳 
	 */
	private String addOrder(Finance info, String hql) {
		if (info.getSort() != null) {
			hql += " order by " + info.getSort() + " " + info.getOrder();
		}
		logger.info("添加了排序条件");
		return hql;
	}

	/**
	 * 
	 * @Title: addWhere
	 * @Description: 添加查询条件
	 * @param info
	 * @param hql
	 * @param params
	 * @return: String
	 */
	private String addWhere(Finance info, String hql, Map<String, Object> params) {
		logger.info("添加了查询条件");
		return hql;
	}
	
	
	/**
	 * 
	 * @Title: uodateBy12
	 * @Description: 每天12点，添加财务表
	 * @see yang.service.FinanceServiceI#uodateBy12()
	 */
	@Override
	public void uodateBy12() {
		List<ChufangInfo> infos=chufangDao.find("from ChufangInfo c");
		for (ChufangInfo chufangInfo : infos) {
			if (chufangInfo.getPatientInfo().getPatientType()>=3) {
				FinanceInfo info=new FinanceInfo();
				info.setFinanceMoney(chufangInfo.getTotalPrice());
				info.setFinanceTime(new Timestamp(System.currentTimeMillis()));
				info.setFinanceType("治疗费");
				info.setId(UUID.randomUUID().toString());
				financeDao.save(info);
			}
			
		}
	}
	//----- get set------
	public BaseDaoI<FinanceInfo> getFinanceDao() {
		return financeDao;
	}

	@Autowired
	public void setFinanceDao(BaseDaoI<FinanceInfo> financeDao) {
		this.financeDao = financeDao;
	}

	public BaseDaoI<ChufangInfo> getChufangDao() {
		return chufangDao;
	}
	@Autowired
	public void setChufangDao(BaseDaoI<ChufangInfo> chufangDao) {
		this.chufangDao = chufangDao;
	}

	
	

}
